using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//using Infragistics.Win.UltraWinTree;
using System.Data.Linq;
using RetailAnalytic.Core;
namespace RetailAnalytic.Shell.UI.Administration
{
    public interface IGUISupp
    {
        UserControl SelectGuiPages(Int16 i);
        bool Load(bool isNew);
        bool Save();
    }
    public class GUISuppG : IGUISupp
    {
        private usrGr _group;
        private bool isNew;
        public GUISuppG(usrGr _g,bool isNew)
        {
            this.isNew = isNew;
            _group = _g;
            ListControl = new List<UserControl>();
            ListControl.Add(new UcGroupMain());
            ListControl.Add(new UcTreePermission());
            ListControl.Add(new UcOther());
            bool bLOAD = ((IGUISupp)this).Load(isNew);
        }
        bool IGUISupp.Load(bool isNew)
        {
            ((UcGroupMain)ListControl[0]).usrgr_name.Text = _group.usrgr_name;
            ((UcGroupMain)ListControl[0]).usrgr_desc.Text = _group.usrgr_desc;
            ((UcGroupMain)ListControl[0]).usrgr_status.Text = _group.usrgr_sts.ToString();
            ((UcTreePermission)ListControl[1]).ShowTreePermission( _group);
            return true;
        }
        bool IGUISupp.Save()
        {
            if (((UcGroupMain)ListControl[0]).usrgr_name.Text.Trim() == String.Empty)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(
                    SR.StringLibrary.KeyEnum.Ask_INPUT_NAME_GROUP,
                    Environment.StackTrace, ""));
                return false;
            }
            _group.usrgr_name = ((UcGroupMain)ListControl[0]).usrgr_name.Text;
            _group.usrgr_desc = ((UcGroupMain)ListControl[0]).usrgr_desc.Text;
            _group.usrgr_sts = Convert.ToInt16(((UcGroupMain)ListControl[0]).usrgr_status.Text);
            
            ((UcTreePermission)ListControl[1]).SavePermission(_group);
            GRetailAnalytic.Instance.GroupList.Save();
            return true;
        }
        public UserControl SelectGuiPages(Int16 i)
        {
            return ListControl[i];
        }
        public List<UserControl> ListControl;
    }

    public class GUISuppU : IGUISupp
    {
        private usr _user;
        private bool isNew;
        public GUISuppU(usr _u, bool isNew)
        {
            this.isNew = isNew;
            _user = _u;
            ListControl = new List<UserControl>();
            ListControl.Add(new ucUserMain());
            ListControl.Add(new UcTreePermission());
            ListControl.Add(new UcOther());
            bool bLOAD = ((IGUISupp)this).Load(isNew);
        }
        bool IGUISupp.Load(bool isNew)
        {
            ((ucUserMain)ListControl[0]).usr_name.Text = _user.usr_name;
            ((ucUserMain)ListControl[0]).usr_pwd.Text = _user.usr_pwd;

            ((ucUserMain)ListControl[0]).usr_firstname.Text = _user.usr_fName;
            ((ucUserMain)ListControl[0]).usr_lastname.Text = _user.usr_lName;
            ((ucUserMain)ListControl[0]).usr_department.Text = _user.usr_dept;
            ((ucUserMain)ListControl[0]).usr_post.Text = _user.usr_post;
            ((ucUserMain)ListControl[0]).FillGroup(GRetailAnalytic.Instance.GroupList, _user);
            ((ucUserMain)ListControl[0]).usr_gr.ValueChanged += delegate { ShowPerm((UcTreePermission)ListControl[1]); };
            if (isNew)
            {
                ((UcTreePermission)ListControl[1]).ShowTreePermission(GRetailAnalytic.Instance.GroupList.GetGroup((short)_user.usr_group));
            }
            else
            {
                ((UcTreePermission)ListControl[1]).ShowTreePermission(_user);
            }
            return true;
        }
        void ShowPerm(UcTreePermission _uc)
        {
            _uc.ShowTreePermission(GRetailAnalytic.Instance.GroupList.GetGroup((short)_user.usr_group));
        }
        bool IGUISupp.Save()
        {


            _user.usr_name = ((ucUserMain)ListControl[0]).usr_name.Text;
            if (((ucUserMain)ListControl[0]).usr_name.Text.Trim() == String.Empty)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.Ask_INPUT_NAME_USER,
                    Environment.StackTrace, ""));
                return false;
            }
            _user.usr_pwd = ((ucUserMain)ListControl[0]).usr_pwd.Text;

            _user.usr_fName = ((ucUserMain)ListControl[0]).usr_firstname.Text;
            _user.usr_lName = ((ucUserMain)ListControl[0]).usr_lastname.Text;
            _user.usr_dept = ((ucUserMain)ListControl[0]).usr_department.Text;
            _user.usr_post = ((ucUserMain)ListControl[0]).usr_post.Text;
            _user.usr_group = Convert.ToInt16(((ucUserMain)ListControl[0]).usr_gr.SelectedItem.DataValue);
            ((UcTreePermission)ListControl[1]).SavePermission(_user);
            GRetailAnalytic.Instance.UserList.Save();
            return true;
        }
        public UserControl SelectGuiPages(Int16 i)
        {
            return ListControl[i];
        }
        public List<UserControl> ListControl;
    }
}
